package com.pms.service.impl;

import com.pms.mapper.DailyTaskMapper;
import com.pms.pojo.DailyTask;
import com.pms.service.DailyTaskService;
import com.pms.util.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;

@Slf4j
@Service
public class DailyTaskServiceImpl implements DailyTaskService {

    @Autowired
    private DailyTaskMapper dailyTaskMapper;

    private HttpServletRequest getCurrentRequest() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            throw new RuntimeException("获取当前请求失败");
        }
        return attributes.getRequest();
    }

    private Long getCurrentUserId() {
        Long userId = JwtUtils.getCurrentUserId(getCurrentRequest());
        if (userId == null) {
            throw new RuntimeException("获取当前用户信息失败，请重新登录");
        }
        return userId;
    }

    @Override
    public List<DailyTask> getCurrentUserTasks() {
        return dailyTaskMapper.selectByUserId(getCurrentUserId());
    }

    @Override
    @Transactional
    public void createTask(DailyTask task) {
        task.setUserId(getCurrentUserId());
        task.setStatus("0"); // 默认未完成状态
        int rows = dailyTaskMapper.insert(task);
        if (rows == 0) {
            throw new RuntimeException("创建任务失败");
        }
    }

    @Override
    @Transactional
    public void updateTask(DailyTask task) {
        // 验证任务所属权
        DailyTask existingTask = dailyTaskMapper.selectById(task.getId());
        if (existingTask == null) {
            throw new RuntimeException("任务不存在");
        }

        Long currentUserId = getCurrentUserId();
        if (!existingTask.getUserId().equals(currentUserId)) {
            throw new RuntimeException("无权操作此任务");
        }

        task.setUserId(currentUserId);
        int rows = dailyTaskMapper.update(task);
        if (rows == 0) {
            throw new RuntimeException("更新任务失败");
        }
    }

    @Override
    @Transactional
    public void updateTaskStatus(Long taskId, String status) {
        // 验证任务所属权
        DailyTask task = dailyTaskMapper.selectById(taskId);
        if (task == null) {
            throw new RuntimeException("任务不存在");
        }

        if (!task.getUserId().equals(getCurrentUserId())) {
            throw new RuntimeException("无权操作此任务");
        }

        int rows = dailyTaskMapper.updateStatus(taskId, status);
        if (rows == 0) {
            throw new RuntimeException("更新任务状态失败");
        }
    }

    @Override
    @Transactional
    public void deleteTask(Long taskId) {
        // 验证任务所属权
        DailyTask task = dailyTaskMapper.selectById(taskId);
        if (task == null) {
            throw new RuntimeException("任务不存在");
        }

        if (!task.getUserId().equals(getCurrentUserId())) {
            throw new RuntimeException("无权操作此任务");
        }

        int rows = dailyTaskMapper.deleteById(taskId);
        if (rows == 0) {
            throw new RuntimeException("删除任务失败");
        }
    }
}